import random
import logging
logger = logging.getLogger('django')

from django.shortcuts import render
from django.views import View
from django.http import JsonResponse,HttpResponse
from django_redis import get_redis_connection
import sys
# print(sys.path)
from libs.yuntongxun.ccp_sms import CCP

sys.path.insert(0,'/home/ubuntu/Desktop/meiduo/meiduo_mall')
from libs.captcha.captcha import captcha
# Create your views here.

# from celery_tasks.sms.tasks import ccp_send_sms_code
class ImageView(View):
    """
    图片验证
    """
    def get(self,request,uuid):

        text,image = captcha.generate_captcha()
        print(text)

        redis_conn = get_redis_connection('verify_code')

        redis_conn.setex('img_%s' % uuid,300,text)

        return HttpResponse(image,content_type='image,jpg')


class MobileView(View):
    def get(self,request,mobile):
        """
        短信验证
        :param request:
        :param mobile:
        :return:
        """
        # 创建reids链接
        conn = get_redis_connection('verify_code')
        # 验证手机号标记  是否频繁发送
        seng_flag_mobile = conn.get('send_flag_%s' % mobile)
        if seng_flag_mobile:
            return JsonResponse({
                'code': 400,
                'errmsg': '发送频繁'
            })


        image_code = request.GET.get('image_code')
        uuid = request.GET.get('image_code_id')

        # 接受参数 效验参数 提取图形验证码 删除验证码 对比验证码 生产验证码 保存验证码 发送短信 相应数据
        if not all([mobile,image_code,uuid]):
            return JsonResponse({
                'code':400,
                'errmsg':"数据不完整"
            })

        # 提取验证码
        image_code_server = conn.get('img_%s' % uuid)
        # 是否过期
        if not image_code_server:
            return JsonResponse({
                'code': 400,
                'errmsg': '验证码已过期'
            })
        # 删除redis中的验证码
        try:
            conn.delete('img_%s' % uuid)
        except Exception as e:
            logger.info(e)
        # 验证图形验证码
        image_code_server = image_code_server.decode()
        if image_code.lower() != image_code_server.lower():
            return JsonResponse({
                'code':400,
                'errmsg':"验证码输入错误"
            })

        # 生成随机验证码
        # randum = random.randint(0,999999)
        # sum = '%06d' % randum
        sum = ''
        for _ in range(6):
            a = str(random.randint(0,9))
            sum += a
        logger.info(sum)
        print(sum)
        # # 保存短信
        # conn.setex('sms_%s' % mobile,300,sum)
        # # 给手机号添加标记 防止频繁发送
        # conn.setex('send_flag_%s' % mobile, 60, 1)
        # 创建管道
        pl = conn.pipeline()
        pl.setex('sms_%s' % mobile,300,sum)
        pl.setex('send_flag_%s' % mobile, 60, 1)
        pl.execute()
        # 发送短信
        # a = CCP().send_template_sms(mobile, [sum, 5], 1)
        # print(a)
        # 通过celery异步执行发送短信  省钱  先不打开
        # ccp_send_sms_code.delay(mobile, sum)

        # 相应结果
        return JsonResponse({
            'code':0,
            'errmsg':"发送成功"
        })